package com.qf.controller.caiwu_controller;

import com.github.pagehelper.PageInfo;
import com.qf.bean.Debt;
import com.qf.bean.PrefundRule;
import com.qf.service.caiwu_service.PrefundRuleService;
import com.qf.utils.caiwu.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 2019/12/11 0011
 * Author->DT
 * sassspringboot
 * 行到水穷处 坐看云起时
 * 模块：预存款规则模块
 */
@RequestMapping("/prefundRule")
@Controller
public class PrefundRuleController {
    @Autowired
    private PrefundRuleService prefundRuleService;
    //跳转展示预存款规则信息
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")Integer index,
                       Model model, String ruleName){
        //0.封装参数
        Map map=new HashMap();
        map.put("index", index);
        map.put("page", 3);
        map.put("ruleName", ruleName);
        //1.分页查询欠款信息列表
        PageInfo<PrefundRule> pageInfo = prefundRuleService.findPrefundRuleListByLimit(map);
        //2.将查询到的信息放入Request域中
        model.addAttribute("pageInfo", pageInfo);
        //3.数据回显信息放入Request域中
        model.addAttribute("ruleName",ruleName);
        return "/caiwu/prefundRule-list";
    }

    //跳转到编辑预存款信息页面
    @RequestMapping("/edit-ui")
    public String editUI(Integer rid,Model model){
        //根据rid查询出规则信息,放到请求域中
        PrefundRule prefundRule = prefundRuleService.selectByPrimaryKey(rid);
        model.addAttribute("prefundRule",prefundRule );
        //转发
        return "/caiwu/prefundRule-edit";
    }

    //编辑预存款信息
    @PostMapping("/edit")
    @ResponseBody
    public ResultVO edit(PrefundRule prefundRule){
        //执行编辑
        int count = prefundRuleService.updateByPrimaryKeySelective(prefundRule);
        if (count==1){
            return new ResultVO(0, "修改预存款信息成功！", null);
        }
        return new ResultVO(1, "修改预存款信息失败！", null);
    }

    //跳转到添加预存款信息页面
    @RequestMapping("/add-ui")
    public String addUI(Integer rid,Model model){
        //转发
        return "/caiwu/prefundRule-add";
    }

    //添加预存款信息
    @PostMapping("/add")
    @ResponseBody
    public ResultVO add(PrefundRule prefundRule){
        //执行添加
        int count = prefundRuleService.insertSelective(prefundRule);
        if (count==1){
            return new ResultVO(0, "添加预存款信息成功！", null);
        }
        return new ResultVO(1, "添加预存款信息失败！", null);
    }


    //查看预存款信息
    @RequestMapping("/view")
    public String view(Integer rid,Model model){
        //根据rid查询出规则信息,放到请求域中
        PrefundRule prefundRule = prefundRuleService.selectByPrimaryKey(rid);
        model.addAttribute("prefundRule",prefundRule );
        return "/caiwu/prefundRule-view";
    }

    //删除预存款信息
    @RequestMapping("/delete")
    @ResponseBody
    public ResultVO delete(Integer rid){
        //执行删除
        int count = prefundRuleService.deleteByPrimaryKey(rid);
        //返回
        if (count==1){
            return new ResultVO(0, "删除预存款信息成功！", null);
        }
        return new ResultVO(1, "删除预存款信息失败！", null);
    }
}
